﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tilver.Coder.CommandExcuteResult;
using Tilver.Coder.Core;
using Tilver.Coder.Utility;

namespace Tilver.Coder.Engine
{
    public class CommandExecuteResultEngine : ICommandExecuteResultEngine
    {
        public void ResponseExecuteNonQuery(StringBuilder response, int tabs, CoderCommand command)
        {
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行SQL语句并返回受影响的行数"));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendTabLine(tabs, "public int ExecuteNonQuery()");
            response.AppendTabLine(tabs, "{");
            response.AppendTabLine(tabs + 1, "return ExecuteNonQuery(null);");
            response.AppendTabLine(tabs, "}");
            response.AppendLine();
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行SQL语句并返回受影响的行数"));
            response.AppendLine(ExtensionUtil.GetCoderTMSummary(tabs));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendTabLine(tabs, "public int ExecuteNonQuery(TransactionManager tm)");
            response.AppendTabLine(tabs, "{");
            response.AppendTabLine(tabs + 1, "return Dao.ExecuteNonQuery(tm);");
            response.AppendTabLine(tabs, "}");
        }

        public void ResponseExecuteScalar(StringBuilder response, int tabs, CoderCommand command)
        {
            if (command.CommandResult.CommandResult != ExecuteResultEnum.ExecuteScalar)
                throw new NotImplementedException();
            ExecuteScalarResult scalar = command.CommandResult as ExecuteScalarResult;

            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行查询语句并返回首行首列数据"));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendFormatLine(tabs, "public {0} ExecuteScalar()", scalar.CSharpType);
            response.AppendTabLine(tabs, "{");
            response.AppendTabLine(tabs + 1, "return ExecuteScalar(null);");
            response.AppendTabLine(tabs, "}");
            response.AppendLine();
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行查询语句并返回首行首列数据"));
            response.AppendLine(ExtensionUtil.GetCoderTMSummary(tabs));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendFormatLine(tabs, "public {0} ExecuteScalar(TransactionManager tm)", scalar.CSharpType);
            response.AppendTabLine(tabs, "{");
            response.AppendTabLine(tabs + 1, "object value =  Dao.ExecuteScalar(tm);");
            response.AppendFormatLine(tabs + 1, " return value == null ? null : ({0})value;", scalar.CSharpType);
            response.AppendTabLine(tabs, "}");
            response.AppendLine();
        }

        public void ResponseExecuteSingle(StringBuilder response, int tabs, CoderCommand command)
        {
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行SQL语句并返回唯一实体对象"));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendFormatLine(tabs, "public {0} ExecuteSingle()", command.EOName);
            response.AppendTabLine(tabs, "{");
            response.AppendTabLine(tabs + 1, "return ExecuteSingle(null);");
            response.AppendTabLine(tabs, "}");
            response.AppendLine();
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行SQL语句并返回唯一实体对象"));
            response.AppendLine(ExtensionUtil.GetCoderTMSummary(tabs));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendFormatLine(tabs, "public {0} ExecuteSingle(TransactionManager tm)", command.EOName);
            response.AppendTabLine(tabs, "{");
            response.AppendFormatLine(tabs + 1, "return Dao.ExecuteReader(tm).MapToSingle<{0}>();", command.EOName);
            response.AppendTabLine(tabs, "}");
        }

        public void ResponseExecuteReader(StringBuilder response, int tabs, CoderCommand command)
        {
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行SQL语句并返回实体对象集合"));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendFormatLine(tabs, "public IEnumerable<{0}> ExecuteReader()", command.EOName);
            response.AppendTabLine(tabs, "{");
            response.AppendTabLine(tabs + 1, "return ExecuteReader(null);");
            response.AppendTabLine(tabs, "}");
            response.AppendLine();
            response.AppendLine(ExtensionUtil.GetCoderSummary(tabs, "执行SQL语句并返回实体对象集合"));
            response.AppendLine(ExtensionUtil.GetCoderTMSummary(tabs));
            response.AppendLine(ExtensionUtil.GetCoderReturnSummary(tabs, string.Empty));
            response.AppendFormatLine(tabs, "public IEnumerable<{0}> ExecuteReader(TransactionManager tm)", command.EOName);
            response.AppendTabLine(tabs, "{");
            response.AppendFormatLine(tabs + 1, "return Dao.ExecuteReader(tm).MapToMutil<{0}>().ToList();", command.EOName);
            response.AppendTabLine(tabs, "}");
        }
    }
}
